package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与文章相关内容
 */
public class ArticleController {
    private static File articleDir;

    static {
        articleDir=new File("./articles");
        if(!articleDir.exists()){
            articleDir.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始发表文章...");
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String content=request.getParameter("content");
        System.out.println(title+","+author+","+content);

        if(title==null || author==null || content==null){
            File file=new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        File articleFile=new File(articleDir,title+".obj");
        if (articleFile.exists()) {
            File file=new File("./webapps/myweb/article_have_tittle.html");
            response.setEntity(file);
            return;
        }

        //2将该文章信息保存
        //将当前用户以一个Article对象形式序列化到文件中：用户名.obj
        Article article=new Article(title,author,content);
        try (
                ObjectOutputStream oos=new ObjectOutputStream(
                        new FileOutputStream(
                                new File(articleDir,title+".obj")
                        )
                );
        ){
            oos.writeObject(article);
            System.out.println("写出完毕！");

            File file=new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        }  catch (IOException e) {
            e.printStackTrace();
        }
        //3设置响应对象给用户回馈注册结果

        System.out.println("文章发表完毕！");
    }


    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始生成动态页面");
        //1读取users目录中的所有用户信息
        List<Article> articleList = new ArrayList<>();
        File[] subs = articleDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File articleFile : subs){
            try (
                    FileInputStream fis = new FileInputStream(articleFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ){
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articleList.forEach(u-> System.out.println(u));

        //2将用户信息拼入一个html页面
        try {
            PrintWriter pw = new PrintWriter(
                    "./articleList.html","UTF-8"
            );

            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("</tr>");

            for(Article a : articleList) {
                pw.println("<tr>");
                pw.println("<td>"+a.getTitle()+"</td>");
                pw.println("<td>"+a.getAuthor()+"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
            pw.flush();
            System.out.println("写出完毕");

            File file = new File("./articleList.html");
            response.setEntity(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //3将页面作为正文响应给浏览器

        System.out.println("动态页面生成完毕");
    }
}
